package edu.sctu.nieyu.day20150618.testone;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

/**
 * Created by user01 on 2015/6/18.
 */
public class URLUtil2 {

    public void getURL() {
        try {
           // 创建URL实例
            URL url = new URL("http://www.baidu.com");
            //通过URL的openStream方法获取url所指示的对象的资源的字节输入流
            InputStream inputStream = url.openStream();
            //将字节流转换为字符流
            InputStreamReader ips = new InputStreamReader(inputStream);
            //创建缓冲流
            BufferedReader buf = new BufferedReader(ips);
            String data = buf.readLine();
            while (data != null) {
                System.out.println(data);
                data = buf.readLine();
            }
            buf.close();
            ips.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

